using Agron.Data.Contracts;
using Agron.Security.ClientLayer;
using Agron.Services.Proxies;

using System;
using System.ComponentModel;
using System.Windows.Forms;


namespace Agron.UI.SmartClient
{
    partial class EditSessionForm : Agron.UI.SmartClient.DialogBase
    {
        private bool _isNewSession;

        /// <summary>
        /// Initializes a new instance of the <see cref="EditSessionForm"/> class.
        /// </summary>
        public EditSessionForm()
        {
            this.InitializeComponent();

            // Init data entity
            base.DataSource = new Session();

            // Set information tooltip
            this.recurrentInfoToolTip.SetToolTip(this.recurrentSessionKryptonCheckBox, Properties.Resources.NewSessionFormRecurrentMeeting);

            // Anonymous methods for time management
            this.startTimeDateTimePicker.ValueChanged += delegate { this.UpdateTimeValues(); };
            this.startTimeDateTimePicker.ValueChanged += delegate { this.UpdateTimeValues(); };

            // Set minimum values
            this.startDateDateTimePicker.MinDate = DateTime.Now;
            this.startTimeDateTimePicker.MinDate = DateTime.Now;
            this.endTimeDateTimePicker.MinDate = DateTime.Now.AddMinutes(5);

            // Set default values
            this.startDateDateTimePicker.Value = new DateTime(DateTime.Now.AddHours(1).Year, DateTime.Now.AddHours(1).Month, DateTime.Now.AddHours(1).Day, DateTime.Now.AddHours(1).Hour, 0, 0);
            this.startTimeDateTimePicker.Value = this.startDateDateTimePicker.Value;
            this.endTimeDateTimePicker.Value = this.startTimeDateTimePicker.Value.AddHours(1d);

            base.Activated += delegate { this.subjectTextBox.Focus(); };
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is new session.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is new session; otherwise, <c>false</c>.
        /// </value>
        public bool IsNewSession
        {
            get { return _isNewSession; }
            set { _isNewSession = value; }
        }

        /// <summary>
        /// Raises the <see cref="E:DataSourceChanged"/> event.
        /// </summary>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected override void OnDataSourceChanged(EventArgs e)
        {
            base.OnDataSourceChanged(e);

            Session session = (base.DataSource as Session);
            if ((null != session) && !string.IsNullOrEmpty(session.Subject))
            {
                DateTime start = session.Start;

                this.startDateDateTimePicker.MinDate = start;
                this.startTimeDateTimePicker.MinDate = start;
                this.endTimeDateTimePicker.MinDate = start.AddMinutes(5d);

                this.startDateDateTimePicker.Value = start;
                this.startTimeDateTimePicker.Value = start;
                this.endTimeDateTimePicker.Value = session.Finish;
            }
        }

        /// <summary>
        /// Updates the time values set in the date time pickers within the data source.
        /// </summary>
        private void UpdateTimeValues()
        {
            ((Session) base.bindingSource.DataSource).Start = new DateTime(
                this.startDateDateTimePicker.Value.Year,
                this.startDateDateTimePicker.Value.Month,
                this.startDateDateTimePicker.Value.Day,
                this.startTimeDateTimePicker.Value.Hour,
                this.startTimeDateTimePicker.Value.Minute,
                this.startTimeDateTimePicker.Value.Second);

            ((Session) base.bindingSource.DataSource).Finish = new DateTime(
                this.startDateDateTimePicker.Value.Year,
                this.startDateDateTimePicker.Value.Month,
                this.startDateDateTimePicker.Value.Day,
                this.endTimeDateTimePicker.Value.Hour,
                this.endTimeDateTimePicker.Value.Minute,
                this.endTimeDateTimePicker.Value.Second);
        }

        /// <summary>
        /// Handles the CheckedChanged event of the requirePasswordCheckBox control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void requirePasswordCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            bool _checked = this.requirePasswordCheckBox.Checked;
            this.passwordLabel.Enabled = _checked;
            this.passwordTextBox.Enabled = _checked;

            if (!this.requirePasswordCheckBox.Checked)
                this.passwordTextBox.Clear();            
        }

        /// <summary>
        /// Handles the ValueChanged event of the startTimeDateTimePicker control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void startTimeDateTimePicker_ValueChanged(object sender, EventArgs e)
        {
            this.endTimeDateTimePicker.Value = this.startTimeDateTimePicker.Value.AddHours(1d);
        }

        /// <summary>
        /// Handles the DoWork event of the acceptActionBackgroundWorker control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.ComponentModel.DoWorkEventArgs"/> instance containing the event data.</param>
        protected override void OnAcceptActionBackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
        {
            base.OnAcceptActionBackgroundWorkerDoWork(sender, e);

            if (this._isNewSession)
                return;

            Session session = (e.Argument as Session);
            if (null != session)
            {
                // Attach user information before continuing
                session.OwnerId = AgronPrincipal.Current.AgronIdentity.Profile.UserId;

                using (SchedulerServiceProxy sessionManager = new SchedulerServiceProxy())
                {
                    sessionManager.CreateSession(session);
                }
            }
        }
    }
}

